import psutil
import time
from playsound import playsound  # 用于播放本地音频文件
from tkinter import messagebox  # 用于弹窗提醒

# 音频文件路径
sound_file = 'alert.mp3'
# 时间间隔，单位为秒
interval = 60

print('检测中...')
time.sleep(interval)

while True:
    battery = psutil.sensors_battery()
    if battery is None:
        print("No battery is found.")
        exit()

    percentage = battery.percent
    # print(f"Battery Percentage: {percentage}%")

    # 低电量提醒，默认低于30%时提醒
    if percentage <= 30:
        # 发出提示
        playsound(sound_file)  # 播放本地音频文件
        # 弹窗警告
        messagebox.showwarning("低电量提醒", f"当前电量为{percentage}%")

    # 休眠一段时间，然后继续循环
    time.sleep(interval)
